/**
 * Created with IntelliJ IDEA.
 * User: derek
 * Date: 6/16/13
 * Time: 12:47 PM
 * To change this template use File | Settings | File Templates.
 */

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class Send {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv)
            throws java.io.IOException, InterruptedException {
       ConnectionFactory factory = new ConnectionFactory();
       factory.setHost("candide");
       Connection connection = factory.newConnection();
       Channel channel = connection.createChannel();

       channel.queueDeclare(QUEUE_NAME, false, false, false, null);
       String message = "Hello World!";
       channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
       System.out.println(" [x] Sent '" + message + "': current time millis: " + System.currentTimeMillis());
       message = "This is message 2";
       channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
       System.out.println(" [x] Sent '" + message + "': current time millis: " + System.currentTimeMillis() );

       Thread.sleep(3000);
       message = "Waited 3 seconds";
       channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
       System.out.println(" [x] Sent '" + message + "': current time millis: " + System.currentTimeMillis() );

       Thread.sleep(2000);
       message = "Waited 2 seconds";
       channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
       System.out.println(" [x] Sent '" + message + "': current time millis: " + System.currentTimeMillis() );

       Thread.sleep(1000);
       message = "Waited 1 seconds";
       channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
       System.out.println(" [x] Sent '" + message + "': current time millis: " + System.currentTimeMillis() );

       System.out.println(" [x] Sending 1000 messages");
       for (int i = 0; i < 1000; i++)
       {
          message = "Message: " + i;
          channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

          if (i % 100 == 0)
             System.out.println(" [x] Sent " + i + " messages, current time millis: " + System.currentTimeMillis() );
       }
       System.out.println(" [x] Finished sending 1000 messages");

       channel.close();
       connection.close();
    }
}
